from django.db import models
from users.models import User


class Problem(models.Model):
    """问题模型类"""
    title = models.CharField(max_length=64, null=True, verbose_name='问题标题')
    content = models.TextField(null=True, verbose_name='内容')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='文章创建时间', null=True)
    author = models.ForeignKey(User, verbose_name='作者')
    browse = models.IntegerField(default=0, verbose_name='浏览量')
    awesome = models.IntegerField(default=0, verbose_name='点赞量')
    reply = models.IntegerField(default=0, verbose_name='回复量')

    class Meta:
        db_table = 'tb_problem'
        verbose_name = '问题'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Answer(models.Model):
    """回答模型类"""
    problem = models.ForeignKey(Problem, verbose_name='问题')
    answer_content = models.TextField(null=True, verbose_name='评论内容')
    answer_time = models.DateTimeField(auto_now_add=True, verbose_name='主评论时间')
    answer_author = models.ForeignKey(User, verbose_name='评论作者')
    answer_awesome = models.IntegerField(default=0, verbose_name='评论点赞量')

    class Meta:
        db_table = 'tb_answer'
        verbose_name = '主评论表'
        verbose_name_plural = verbose_name
        ordering = ['answer_time']


class ChildAnswer(models.Model):
    """评论模型类"""
    answer = models.ForeignKey(Answer, verbose_name='主评论')
    child_content = models.TextField(null=True, verbose_name='子评论内容')
    child_time = models.DateTimeField(auto_now_add=True, verbose_name='子评论时间')
    child_author = models.ForeignKey(User, verbose_name='子评论作者')
    child_reply_user = models.ForeignKey('self', verbose_name='孙评论作者', blank=True, null=True)

    class Meta:
        db_table = 'tb_child_answer'
        verbose_name = '子评论表'
        verbose_name_plural = verbose_name


# 标签模型类#
class Label(models.Model):
    label_name = models.CharField(max_length=32, verbose_name='标签名')
    label_describe = models.TextField(verbose_name='标签描述')
    hanswer = models.ManyToManyField(Problem, through='ProblemLabelRelation')

    class Meta:
        db_table = 'tb_label'
        verbose_name = '标签表'
        verbose_name_plural = verbose_name


class UsreProblemLikesRelation(models.Model):
    """用户 问题点赞关系表"""
    Problem = models.ForeignKey(Problem, verbose_name='问题')
    User = models.ForeignKey(User, verbose_name='用户')

    class Meta:
        db_table = 'usre_problem_likes_relation'
        verbose_name = '用户问题点赞关系表'
        verbose_name_plural = verbose_name


class UsreAnswerLikesRelation(models.Model):
    """用户 回答点赞关系表"""
    Answer = models.ForeignKey(Answer, verbose_name='回答')
    User = models.ForeignKey(User, verbose_name='用户')
    is_value = models.IntegerField(default=0, verbose_name='收藏')

    class Meta:
        db_table = 'usre_answer_likes_relation'
        verbose_name = '用户回答点赞关系表'
        verbose_name_plural = verbose_name


class ProblemLabelRelation(models.Model):
    """问题 标签关系表"""
    Label = models.ForeignKey(Label, verbose_name='标签')
    Problem = models.ForeignKey(Problem, verbose_name='问题')

    class Meta:
        db_table = 'problem_label_relation'
        verbose_name = '问题标签关系表'
        verbose_name_plural = verbose_name
